package views;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.example.zx.theline.R;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import Utils.DictionariesUtil;
import app.MyApplication;
import bean.JhshSearchBean;
import greendao.UnitInformationDao;
import greendao.entity.UnitInformation;

/**
 * Created by Ren on 2017/8/21.
 */

public class JhshSearchView extends RelativeLayout implements View.OnClickListener {

    //    TextView jhnyks, jhnyjs;
    EditText yjhh, xm;
    EditText dw;
    RadioButton qb, zs, fzs;
    LinearLayout xm_layout;
    RadioGroup sfzs;


    TextView lb, zt;
    Spinner spinner_lb, spinner_zt;

    /**
     * 按钮
     */
    TextView cancel, delect, confirm;

    JhshSearchBean mJhshSearchBean;
    OnClick mOnClick;
    private ListPopWindow ztListPopWindow;
    private ListPopWindow lbListPopWindow;
    private List ztlist = new ArrayList();
    private List lblist = new ArrayList();
    private ListPopWindow dwhbmPop;
    private List<UnitInformation> mAutoSource = new ArrayList<>();

    int flag = 0;
    public static final int SHYJH = 1;
    public static final int SHYWC = 2;
    public static final int SPYWC = 3;
    public static final int YJHCX = 4;

    public JhshSearchView(Context context, int flag) {
        super(context);
        this.flag = flag;
        initView();
        initDate();
        initListener();
    }

    public JhshSearchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
        initDate();
        initListener();
    }

    public JhshSearchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
        initDate();
        initListener();
    }

    private void initView() {
        LayoutInflater.from(getContext()).inflate(R.layout.jhshsearchview, this);
//        jhnyks = (TextView) findViewById(R.id.jhnyks);
//        jhnyjs = (TextView) findViewById(R.id.jhnyjs);
        yjhh = (EditText) findViewById(R.id.yjhh);
        xm = (EditText) findViewById(R.id.xm);
        dw = (EditText) findViewById(R.id.dwhbm);
        lb = (TextView) findViewById(R.id.lb);
        spinner_lb = (Spinner) findViewById(R.id.spinner_lb);
        zt = (TextView) findViewById(R.id.zt);
        spinner_zt = (Spinner) findViewById(R.id.spinner_zt);
        qb = (RadioButton) findViewById(R.id.qb);
        zs = (RadioButton) findViewById(R.id.zs);
        fzs = (RadioButton) findViewById(R.id.fzs);
        xm_layout = (LinearLayout) findViewById(R.id.xm_layout);
        sfzs = (RadioGroup) findViewById(R.id.sfzs);


        cancel = (TextView) findViewById(R.id.cancel);
        delect = (TextView) findViewById(R.id.delect);
        confirm = (TextView) findViewById(R.id.confirm);


        if (mJhshSearchBean == null) {
            mJhshSearchBean = new JhshSearchBean();
        }
        if (dwhbmPop == null) {
            dwhbmPop = new ListPopWindow.Builder(getContext())
                    .setList(DictionariesUtil.getUnitList())
                    .setOutsideTouchable(false)
                    .setParentView(dw)
                    .setOnListItemCilck(new ListPopWindow.OnListItemCilck() {
                        @Override
                        public void OnListItemCilck(int position) {
                            dw.setText(DictionariesUtil.getUnitList().get(position).toString());
                            dwhbmPop.dismiss();
                        }
                    }).builder();
        }

    }

    private void initDate() {

        lblist = Arrays.asList(getContext().getResources().getStringArray(R.array.lb));


        dw.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 0) {
                    return;
                } else {
                    if (!dwhbmPop.getWindow().isShowing()) {
                        dwhbmPop.show();
                    }
                    mAutoSource = MyApplication.getDaoSession()
                            .getUnitInformationDao()
                            .queryBuilder()
                            .where(UnitInformationDao.Properties.LjyyxDwxxName.like("%" + s + "%")).list();
                    if (mAutoSource.size() == 0) {
                        UnitInformation un = new UnitInformation();
                        un.setLjyyxDwxxName("没有匹配的单位");
                        mAutoSource.add(un);
                    }
                    dwhbmPop.setList(mAutoSource);
                }
            }
        });

        zt.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ztListPopWindow == null) {
                    ztListPopWindow = new ListPopWindow.Builder(getContext()).setList(ztlist)
                            .setParentView(zt)
                            .setOnListItemCilck(new ListPopWindow.OnListItemCilck() {
                                @Override
                                public void OnListItemCilck(int position) {
                                    if (position == 0) {
                                        zt.setText("");
                                    } else {
                                        zt.setText(ztlist.get(position).toString());
                                    }
                                    ztListPopWindow.dismiss();
                                }
                            }).builder();
                }
                ztListPopWindow.show();
            }
        });

        lb.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (lbListPopWindow == null) {
                    lbListPopWindow = new ListPopWindow.Builder(getContext()).setList(lblist)
                            .setParentView(lb)
                            .setOnListItemCilck(new ListPopWindow.OnListItemCilck() {
                                @Override
                                public void OnListItemCilck(int position) {
                                    if (position == 0) {
                                        lb.setText("");
                                    } else {
                                        lb.setText(lblist.get(position).toString());
                                    }

                                    lbListPopWindow.dismiss();
                                }
                            }).builder();
                }
                lbListPopWindow.show();
            }
        });


    }

    public void setZtlistFlag(int flag) {
        if (flag == SHYJH) {
            ztlist = Arrays.asList(getContext().getResources().getStringArray(R.array.zt_sh));
        }
        if (flag == SHYWC) {
            ztlist = Arrays.asList(getContext().getResources().getStringArray(R.array.yjhywc_zt));
        }
        if (flag == SPYWC) {
            ztlist = Arrays.asList(getContext().getResources().getStringArray(R.array.yjhywc_zt));
        }
        if (flag == YJHCX) {
            ztlist = Arrays.asList(getContext().getResources().getStringArray(R.array.yjhcx_zt));
        }
    }


    private void initListener() {
//        jhnyks.setOnClickListener(this);
//        jhnyjs.setOnClickListener(this);
        cancel.setOnClickListener(this);
        delect.setOnClickListener(this);
        confirm.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
//            case R.id.jhnyjs:
//                TimePickerView.getInstanceEndMonth(getContext(), new TimePickerView.OnTimeSelectListener() {
//                    @Override
//                    public void onTimeSelect(Date date, View v) {
//                        jhnyjs.setText(getTime(date));
//                    }
//                }).show();
//                break;
//            case R.id.jhnyks:
//                TimePickerView.getInstanceEndMonth(getContext(), new TimePickerView.OnTimeSelectListener() {
//                    @Override
//                    public void onTimeSelect(Date date, View v) {
//                        jhnyks.setText(getTime(date));
//                    }
//                }).show();
//                break;
            case R.id.cancel:
                setVisibility(GONE);
                break;
            case R.id.delect:
//                jhnyks.setText("");
//                jhnyjs.setText("");
                yjhh.setText("");
                dw.setText("");
                xm.setText("");
                lb.setText("");
                zt.setText("");
                mJhshSearchBean.clear();
                break;
            case R.id.confirm:
                mJhshSearchBean.setJhh(yjhh.getText().toString());
//                mJhshSearchBean.setJhnyks(jhnyks.getText().toString());
//                mJhshSearchBean.setJhnyjs(jhnyjs.getText().toString());
                mJhshSearchBean.setLb(lb.getText().toString());
                mJhshSearchBean.setXm(xm.getText().toString());
                if (dw.getText().length() != 0) {
                    List<UnitInformation> list = MyApplication.getDaoSession().getUnitInformationDao().queryBuilder()
                            .where(UnitInformationDao.Properties.LjyyxDwxxName.eq(dw.getText().toString())).list();
                    if (list.size() != 0) {
                        mJhshSearchBean.setDwhbmId(list.get(0).getLjyyxDwxxId());
                    } else {
                        //如果没有对应的单位 则设置搜索ID为-1
                        mJhshSearchBean.setDwhbmId("-1");
                    }
                }
                mJhshSearchBean.setZt(zt.getText().toString());
                if (zs.isChecked()) {
                    mJhshSearchBean.setSfzs("主审");
                }
                if (fzs.isChecked()) {
                    mJhshSearchBean.setSfzs("非主审");
                }
                if (qb.isChecked()) {
                    mJhshSearchBean.setSfzs("");
                }
                if (mOnClick != null) {
                    mOnClick.onConfirmListener(mJhshSearchBean);
                }
                setVisibility(GONE);
                break;
        }
    }

    public void hideXmLayout() {
        xm_layout.setVisibility(GONE);
    }

    public void hideSfzsLayout() {
        sfzs.setVisibility(GONE);
    }

    public void setConfirmListener(OnClick onClick) {
        this.mOnClick = onClick;
    }

    public interface OnClick {
        void onConfirmListener(JhshSearchBean Util);
    }

    //对外暴露一个方法设置默认筛选时间
    public void setDefaultDate(JhshSearchBean bean) {
        mJhshSearchBean = bean;
//        if (mJhshSearchBean.getJhnyks() != null && mJhshSearchBean.getJhnyjs() != null) {
//            jhnyks.setText(mJhshSearchBean.getJhnyks());
//            jhnyjs.setText(mJhshSearchBean.getJhnyjs());
//        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return true;
    }

    private String getTime(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        return format.format(date);
    }
}
